<?php

define( "WEB_STAT_LOAD_MAIN_SITE", 0 );
define( "WEB_STAT_LOGIN_USER", 1 );
define( "WEB_STAT_RESERVATION_DONE", 2 );
define( "WEB_STAT_RESERVATION_CANCELED", 3 );
define( "WEB_STAT_USER_REGISTERED", 4 );
define( "WEB_STAT_FORGOTTEN_PASSWORD", 5 );
define( "WEB_STAT_PASSWORD_CHANGED", 6 );

class WebStat
{
    static private function logStatistic( $actionType, $actionHexEncodedData )
    {    
        $db = Db::db();
        $dbConnection = $db->connection();
        if (is_null( $dbConnection ))
        {
            logError( __METHOD__ . ": " . $db->getErrorStr() );
            return;
        }
    
        try
        {
            $statement = $dbConnection->prepare( "SELECT nextval('webstatsequence')" );
            $statement->execute();
            $obj= $statement->fetch( PDO::FETCH_OBJ );
            if (! $obj)
            {
                logError( __METHOD__ . ": Unknown error with id" );
                return;
            }
    
            $statement = $dbConnection->prepare( "INSERT INTO webstats( id, actiondatetime, event, actiondata ) VALUES( ?, now(), ?, ? )" );
            $statement->bindParam( 1, $obj->nextval, PDO::PARAM_INT );
            $statement->bindParam( 2, $actionType, PDO::PARAM_INT );
            $statement->bindParam( 3, $actionHexEncodedData, PDO::PARAM_STR );
            $statement->execute();
            $db->commit();
        }
        catch(PDOException $e)
        {
            logError( __METHOD__ . ": " . $e->getMessage() );
            return;
        }
    }
    
    static private function prettyDate( $date )
    {
        $ar = explode( "-", $date );
        for ($i = 1; $i < 3; $i ++)
        {
            $ar[i] = $ar[i] < 10 ? sprintf( "0%d", $ar[i] ) : sprintf( "%d", $ar[i] );
        }
        
        return sprintf( "%d.%d.%d", $ar[2], $ar[1], $ar[0] );
    }
    
    static public function logLoadMainSite()
    {
        $dbStr = strToHex( getRealIpAddr() );
        WebStat::logStatistic( WEB_STAT_LOAD_MAIN_SITE, $dbStr );
    }
    
    static public function logLoginUser( $emailAddress )
    {
        $dbStr = strToHex( $emailAddress );
        WebStat::logStatistic( WEB_STAT_LOGIN_USER, $dbStr );
    }
    
    static public function logReservationDone( $emailAddress, $reservationDate, $courtNumber, $reservationTime )
    {
        $dbStr = strToHex( $emailAddress ) . "|" . strToHex( WebStat::prettyDate( $reservationDate ) ) . "|" . strToHex( $courtNumber ) . "|" . strToHex( $reservationTime );
        WebStat::logStatistic( WEB_STAT_RESERVATION_DONE, $dbStr );
    }
    
    static public function logReservationCanceled( $emailAddress, $reservationDate, $courtNumber, $reservationTime )
    {
        $dbStr = strToHex( $emailAddress ) . "|" . strToHex( WebStat::prettyDate( $reservationDate ) ) . "|" . strToHex( $courtNumber ) . "|" . strToHex( $reservationTime );
        WebStat::logStatistic( WEB_STAT_RESERVATION_CANCELED, $dbStr );
    }
    
    static public function logUserRegistered( $emailAddress )
    {
        $dbStr = strToHex( $emailAddress );
        WebStat::logStatistic( WEB_STAT_USER_REGISTERED, $dbStr );
    }
    
    static public function logForgottenPassword( $emailAddress )
    {
        $dbStr = strToHex( $emailAddress );
        WebStat::logStatistic( WEB_STAT_FORGOTTEN_PASSWORD, $dbStr );
    }
    
    static public function logPasswordChanged( $emailAddress )
    {
        $dbStr = strToHex( $emailAddress );
        WebStat::logStatistic( WEB_STAT_PASSWORD_CHANGED, $dbStr );
    }
}